package com.naiterui.ehp.bs.file.controller.inner;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.mvc.RequestHeaderDecode;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bs.file.api.bo.FileBO;
import com.naiterui.ehp.bs.file.api.bo.FileParamBO;
import com.naiterui.ehp.bs.file.service.FileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 文件相关
 *
 * @author yangfuya
 * @since 2021/9/9
 */
@Slf4j
@Api(tags = SwaggerTags.BASE_INNER_FILE)
@RestController
@RequiredArgsConstructor
public class InnerFileController {

    private final FileService fileService;

    @ApiOperation("生成预览Url")
    @PostMapping("/inner/generate/preview")
    public FileBO generatePreview(@RequestBody FileParamBO fileParamBO, @RequestHeaderDecode String operator) {
        log.info("生成预览Url param:{}, operator:{}", fileParamBO, operator);
        return fileService.generatePreview(fileParamBO, operator);
    }

    @ApiOperation("批量生成预览Url")
    @PostMapping("/inner/batch/generate/preview")
    public ResponseVO<Map<String, FileBO>> batchGeneratePreview(@RequestBody FileParamBO fileParamB,
        @RequestHeaderDecode String operator) {
        log.info("批量生成预览Url param:{}, operator:{}", fileParamB, operator);
        return PublicService.returnResponseVO(fileService.batchGeneratePreview(fileParamB, operator));
    }

    @ApiOperation("生成下载Url")
    @PostMapping("/inner/generate/download")
    public FileBO generateDownload(@RequestBody FileParamBO fileParamB, @RequestHeaderDecode String operator) {
        log.info("生成下载Url param:{}, operator:{}", fileParamB, operator);
        return fileService.generateDownload(fileParamB, operator);
    }

    @ApiOperation("获取文件真实路径")
    @ApiImplicitParam(name = "url", value = "安全url", required = true, dataType = "string", paramType = "form")
    @GetMapping("/inner/real/path")
    public String getRealPath(String url) {
        log.info("获取文件真实路径 url:{}", url);
        return fileService.getRealPath(url);
    }

    @ApiOperation("批量获取文件真实路径")
    @ApiImplicitParam(name = "urls", value = "安全url列表", required = true, dataType = "list", paramType = "body")
    @PostMapping("/inner/batch/real/path")
    public Map<String, FileBO> batchRealPath(@RequestBody List<String> urls) {
        log.info("批量获取文件真实路径 urls:{}", JsonMapper.toJson(urls));
        return fileService.batchRealPath(urls);
    }
}